﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.User
{
    /// <summary>
    /// Page_List.xaml 的交互逻辑
    /// </summary>
    public partial class Page_List : Page
    {
        ViewModel.UserViewModel vm;

        Model.DBContext dbContext = new Model.DBContext();
        Model.Repository<Model.Users> RepUser = new Model.Repository<Model.Users>();
        public Page_List()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.UserViewModel;
            vm.UserList = new  ObservableCollection<Model.Users>( RepUser.GetList());

           
        }

        private void btn_New_Click(object sender, RoutedEventArgs e)
        {
            View.User.AddUser win = new AddUser();
            win.ShowDialog();
        }

        private void btn_Del_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrUser ==null)
            {
                Growl.WarningGlobal("请选择要删除的用户！");
                return;
            }

            RepUser.Delete(vm.CurrUser);
            Growl.SuccessGlobal("删除成功！"); 
            vm.UserList = new ObservableCollection<Model.Users>(RepUser.GetList());
        }

        private void SearchBar_SearchStarted(object sender, HandyControl.Data.FunctionEventArgs<string> e)
        {
            vm.UserList = new ObservableCollection<Model.Users>(dbContext.DB.Queryable<Model.Users>().Where(u=>u.Name.Contains( e.Info) || u.Id.Contains(e.Info) || u.Phone .Contains(e.Info)).ToList());
        }

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {
            EditUser win = new EditUser();
            win.Show();
        }
    }
}
